package com.lhkj.ct.admin.modules.psychology.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.lhkj.ct.base.annntation.Log;
import com.lhkj.ct.base.constants.Constants;
import com.lhkj.ct.base.enums.ActionType;
import com.lhkj.ct.base.model.dto.PageQuery;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.meta.modules.gauge.model.dto.RemindDto;
import com.lhkj.ct.meta.modules.gauge.service.QrCodeService;
import com.lhkj.ct.meta.modules.gauge.service.RemindService;
import com.lhkj.ct.meta.modules.psychology.model.bo.DimensionBo;
import com.lhkj.ct.meta.modules.psychology.model.dto.ProcessDto;
import com.lhkj.ct.meta.modules.psychology.model.dto.ScaleSubmitDto;
import com.lhkj.ct.meta.modules.psychology.model.vo.PsyRecordsVo;
import com.lhkj.ct.meta.modules.psychology.model.vo.ViewAnswerVo;
import com.lhkj.ct.meta.modules.psychology.service.PsyAnswerService;
import com.lhkj.ct.meta.modules.psychology.service.PsyRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.UriComponentsBuilder;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *     测验记录
 * </p>
 *
 * @author jobob
 * @since 2023-10-12
 */
@RestController
@RequestMapping("api/plat/psychology/record")
@Api(tags = {"2.2测验记录"})
@Validated
public class PsyRecordController {

    @Resource
    private PsyRecordService psyRecordService;

    @Resource
    private QrCodeService qrCodeService;

    @Resource
    private RemindService remindService;

    @Resource
    private PsyAnswerService psyAnswerService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "测验列表")
    @GetMapping("list")
//    //@RequiresPermissions(value = {"patient:unit:all"}, logical = Logical.OR)
    public ActionResult<IPage<PsyRecordsVo>> selectPatUnitPage(PsyRecordsVo record, PageQuery pageQuery) {
        return ActionResult.success(psyRecordService.selectPages(record, pageQuery));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "获取测验的量表，题目等信息", notes = "rId:报告id, info:量表信息, questions:题目, answerJson:答案, costTime:计时")
    @GetMapping("{allotId}/get")
//    //@RequiresPermissions(value = {"patient:unit:all"}, logical = Logical.OR)
    public ActionResult<Map<String, Object>> get(@PathVariable String allotId) {
        return ActionResult.success(psyRecordService.selectScaleWithQuestionsByAllotId(allotId, false));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "查看作答情况")
    @GetMapping("{allotId}/view")
//    //@RequiresPermissions(value = {"patient:unit:all"}, logical = Logical.OR)
    public ActionResult<ViewAnswerVo> view(@PathVariable String allotId) {
        return ActionResult.success(psyRecordService.viewAnswer(allotId));
    }

    @PostMapping("submit")
    @ApiOperation(value = "作答提交")
    @Log(title = "测验记录作答", actionType = ActionType.INSERT)
//    //@RequiresPermissions(value = {"XLCP:LBFP:DELETE"})
    public ActionResult<List<DimensionBo>> submit(@Valid @RequestBody ScaleSubmitDto scaleSubmitDto) {
        psyAnswerService.submit(scaleSubmitDto);
        return ActionResult.success();
    }

    @PostMapping("process")
    @ApiOperation(value = "保存作答进度")
//    //@RequiresPermissions(value = {"XLCP:LBFP:DELETE"})
    public ActionResult<Void> saveProcess(@Validated @RequestBody ProcessDto process) {
        return ActionResult.judge(psyAnswerService.saveProcess(process));
    }

    @Validated
    @PostMapping("delete")
//    //@RequiresPermissions(value = {"XLCP:LBFP:DELETE"})
    @ApiOperation(value = "撤销接口", notes = "支持批量撤销")
    @Log(title = "测验记录", actionType = ActionType.DELETE)
    public ActionResult<Void> delete(@RequestBody @NotEmpty(message = "撤销的编号不能为空") List<String> ids) {
        int count = psyRecordService.removeByIds(ids);
        if (count == 0) {
            return ActionResult.fail("撤销失败");
        }
        return ActionResult.success("撤销完成，共撤销【" + count + "】条");
    }

    @PostMapping(value = "remind")
    @ApiOperation(value = "提醒接口")
//    //@RequiresPermissions(value = {"XLCP:LBFP:FPJL:REMIND"}, logical = Logical.OR)
    public ActionResult<Void> remind(@Valid @RequestBody RemindDto entity) {
        remindService.remind(entity);
        return ActionResult.success("提醒成功");
    }

    @GetMapping(value = "{rId}/qrCode")
    @ApiOperation(value = "二维码接口")
    public ActionResult<String> getQrCode(@PathVariable String rId) throws UnsupportedEncodingException {
        Map<String, Object> queryParams = new HashMap<>();
        UriComponentsBuilder redirectBuilder = UriComponentsBuilder.fromUriString(Constants.SERVICE_BASE_URL).path("/wechat/#/");
        redirectBuilder.queryParam("allotId", rId);
        String url = redirectBuilder.build().toUriString();
        queryParams.put("url", URLEncoder.encode(url, Constants.UTF8));
        return ActionResult.success("操作成功",qrCodeService.generateQrCode("/ct/api/wechat/scan/qrcode", queryParams));
    }

}
